//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm;
using EntLibContrib.Resource.Properties;

namespace EntLibContrib.Resource.Configuration.Manageability
{
	/// <summary>
	/// Provides a default implementation for <see cref="CustomResourceProviderData"/> that
	/// splits policy overrides processing and WMI objects generation, performing appropriate logging of 
	/// policy processing errors.
	/// </summary>
	public class CustomResourceProviderDataManageabilityProvider
		: CustomProviderDataManageabilityProvider<CustomResourceProviderData>
	{
		#region Constants
		/// <summary>
		/// The name of the attributes property.
		/// </summary>
		public new const String AttributesPropertyName = CustomProviderDataManageabilityProvider<CustomResourceProviderData>.AttributesPropertyName;
		/// <summary>
		/// The name of the provider type property.
		/// </summary>
		public new const String ProviderTypePropertyName = CustomProviderDataManageabilityProvider<CustomResourceProviderData>.ProviderTypePropertyName;
		#endregion

		#region Construction
		/// <summary>
		/// Initializes a new instance of the <see cref="CustomResourceProviderDataManageabilityProvider"/> class.
		/// </summary>
		public CustomResourceProviderDataManageabilityProvider()
			: base("")
		{
			CustomResourceProviderDataWmiMapper.RegisterWmiTypes();
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Adds the ADM instructions that describe the policies that can be used to override the properties of
		/// a specific instance of the configuration element type managed by the receiver.
		/// </summary>
		/// <param name="contentBuilder">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
		/// <param name="configurationObject">The configuration object instance.</param>
		/// <param name="configurationSource">The configuration source from where to get additional configuration
		/// information, if necessary.</param>
		/// <param name="elementPolicyKeyName">The key for the element's policies.</param>
		/// <remarks>
		/// The default implementation for this method creates a policy, using
		/// <see cref="P:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.ElementPolicyNameTemplate"/> to create the policy name and invoking
		/// <see cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.AddElementAdministrativeTemplateParts(Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder,`0,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource,System.String)"/>
		/// to add the policy parts.
		/// Subclasses managing objects that must not create a policy must override this method to just add the parts.
		/// </remarks>
		protected override void AddAdministrativeTemplateDirectives(AdmContentBuilder contentBuilder,
																																CustomResourceProviderData configurationObject,
																																IConfigurationSource configurationSource,
																																String elementPolicyKeyName)
		{
			// parts for providers are part of their resource manager's policies
			AddElementAdministrativeTemplateParts(contentBuilder,
																						configurationObject,
																						configurationSource,
																						elementPolicyKeyName);
		}

		/// <summary>
		/// Adds the ADM parts that represent the properties of
		/// a specific instance of the configuration element type managed by the receiver.
		/// </summary>
		/// <param name="contentBuilder">The <see cref="T:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder"/> to which the Adm instructions are to be appended.</param>
		/// <param name="configurationObject">The configuration object instance.</param>
		/// <param name="configurationSource">The configuration source from where to get additional configuration
		/// information, if necessary.</param>
		/// <param name="elementPolicyKeyName">The key for the element's policies.</param>
		/// <remarks>
		/// Subclasses that manage custom provider's configuration objects with additional properties may
		/// override this method to add the corresponding parts.
		/// </remarks>
		/// <seealso cref="M:Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.ConfigurationElementManageabilityProviderBase`1.AddAdministrativeTemplateDirectives(Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability.Adm.AdmContentBuilder,`0,Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource,System.String)"/>
		protected override void AddElementAdministrativeTemplateParts(AdmContentBuilder contentBuilder,
																																	CustomResourceProviderData configurationObject,
																																	IConfigurationSource configurationSource,
																																	String elementPolicyKeyName)
		{
			contentBuilder.AddEditTextPart(InternalResources.CustomResourceProviderDataTypePartName,
																		elementPolicyKeyName,
																		ProviderTypePropertyName,
																		configurationObject.Type.AssemblyQualifiedName,
																		1024,
																		true);

			contentBuilder.AddEditTextPart(InternalResources.CustomResourceProviderDataAttributesPartName,
																		elementPolicyKeyName,
																		AttributesPropertyName,
																		GenerateAttributesString(configurationObject.Attributes),
																		1024,
																		false);
		}

		/// <summary>
		/// Creates the <see cref="ConfigurationSetting"/> instances that describe the
		/// configurationObject.
		/// </summary>
		/// <param name="configurationObject">The configuration object for instances that must be managed.</param>
		/// <param name="wmiSettings">A collection to where the generated WMI objects are to be added.</param>
		protected override void GenerateWmiObjects(CustomResourceProviderData configurationObject, ICollection<ConfigurationSetting> wmiSettings)
		{
			CustomResourceProviderDataWmiMapper.GenerateWmiObjects(configurationObject, wmiSettings);
		}
		#endregion
	}
}
